<?php

namespace Nece\Sdk\Coze\Entity;

/**
 * 消息内容
 *
 * @author gjw
 * @created 2025-04-24 17:23:08
 */
class EnterMessage
{
    // 发送这条消息的实体
    const ROLE_USER = 'user';
    const ROLE_ASSISTANT = 'assistant';

    // 消息类型
    const TYPE_QUESTION = 'question';
    const TYPE_ANSWER = 'answer';
    const TYPE_FUNCTION_CALL = 'function_call';
    const TYPE_TOOL_RESPONSE = 'tool_response';
    const TYPE_FOLLOW_UP = 'follow_up';
    const TYPE_VERBOSE = 'verbose';

    // 消息内容的类型
    const CONTENT_TYPE_TEXT = 'text';
    const CONTENT_TYPE_OBJECT_STRING = 'object_string';

    private $role;
    private $type;
    private $content_type;
    private $messages = array();
    private $meta_data;

    /**
     * 构造
     *
     * @author gjw
     * @created 2025-04-24 17:23:17
     *
     * @param Message $message
     * @param string $content_type
     * @param string $role
     * @param string $type
     */
    public function __construct(Message $message, string $content_type = self::CONTENT_TYPE_TEXT, string $role = self::ROLE_USER, string $type = self::TYPE_QUESTION)
    {
        $this->role = $role;
        $this->type = $type;
        $this->content_type = $content_type;
        $this->addMessage($message);
    }

    /**
     * 添加消息
     *
     * @author gjw
     * @created 2025-04-24 17:23:31
     *
     * @param Message $message
     * @return this
     */
    public function addMessage(Message $message)
    {
        $this->messages[] = $message;
        return $this;
    }

    /**
     * 添加附加数据
     *
     * @author gjw
     * @created 2025-04-24 17:23:40
     *
     * @param MetaData $meta_data
     * @return this
     */
    public function setMetaData(MetaData $meta_data)
    {
        $this->meta_data = $meta_data;
        return $this;
    }

    /**
     * 转数组
     *
     * @author gjw
     * @created 2025-04-24 17:23:55
     *
     * @return array
     */
    public function toArray()
    {
        $content = '';
        $contents = array();
        foreach ($this->messages as $message) {
            if ($this->content_type == self::CONTENT_TYPE_TEXT) {
                $contents[] = $message->toString();
            } else {
                $contents[] = $message->toArray();
            }
        }

        if ($this->content_type == self::CONTENT_TYPE_TEXT) {
            $content = implode("\n", $contents);
        } else {
            $content = json_encode($contents);
        }

        $data = array(
            'role' => $this->role,
            'type' => $this->type,
            'content_type' => $this->content_type,
            'content' => $content,
        );

        if ($this->meta_data) {
            $data['meta_data'] = $this->meta_data->toJsonString();
        }

        return $data;
    }

    /**
     * 转json字符串
     *
     * @author gjw
     * @created 2025-04-24 17:24:06
     *
     * @return string
     */
    public function toJsonString()
    {
        return json_encode($this->toArray());
    }

    /**
     * 转字符串
     *
     * @author gjw
     * @created 2025-04-24 17:24:17
     *
     * @return string
     */
    public function toString()
    {
        return $this->toJsonString();
    }
}
